<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理" lazy>
          <el-button
            type="primary"
            size="small"
            @click="addName"
          >+ 新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号" type="index" />
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="permissionEvent(row)"
                  >分配权限</el-button>
                  <el-button type="text" @click="edit(row)">编辑</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 5, 10, 20]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <company-info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <Add ref="add" v-model="show" />
    <Permission ref="Permission" :is-show.sync="showPermission" />
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import Add from './components/Add.vue'
import Permission from './components/Permission.vue'
export default {
  components: {
    companyInfo: () => import('./components/companyInfo.vue'),
    Add,
    Permission
  },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      show: false,
      showPermission: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // pagesize
    handleSizeChange(val) {
      this.page.pagesize = val
      this.page.page = 1
      this.getData()
    },
    // 当前页
    handleCurrentChange(val) {
      this.page.page = val
      this.getData()
    },
    // 获取数据
    async getData() {
      const { data } = await sysRole(this.page)
      this.list = data.rows
      this.page.total = data.total
    },
    //  打开弹出层
    addName() {
      this.show = true
      this.$refs.add.mode = 'add'
    },
    // 删除
    del(row) {
      this.$confirm('确定要删除该角色吗', '提示')
        .then(async() => {
          await sysRoleDel(row.id)
          this.$message.success('删除成功')
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    // 编辑
    async edit(row) {
      this.show = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 分配权限
    permissionEvent(row) {
      this.showPermission = true
      this.$refs.Permission.getPremissionInfo(row.id)
    }
  }
}
</script>

<style></style>
